Jupyter Notebook上で3Dコロプレスマップを描写

ポイント

描写例

準備

  • cesiumpyのインストール
  • GeoJsonのダウンロード
In [1]:
! pip install cesiumpy
import cesiumpy
Requirement already satisfied: cesiumpy in /usr/local/lib/python3.6/dist-packages (0.3.3)
Requirement already satisfied: geopy>=1.11.0 in /usr/local/lib/python3.6/dist-packages (from cesiumpy) (1.16.0)
Requirement already satisfied: six in /usr/lib/python3/dist-packages (from cesiumpy) (1.11.0)
Requirement already satisfied: traitlets in /usr/local/lib/python3.6/dist-packages (from cesiumpy) (4.3.2)
Requirement already satisfied: geographiclib<2,>=1.49 in /usr/local/lib/python3.6/dist-packages (from geopy>=1.11.0->cesiumpy) (1.49)
Requirement already satisfied: ipython-genutils in /usr/local/lib/python3.6/dist-packages (from traitlets->cesiumpy) (0.2.0)
Requirement already satisfied: decorator in /usr/local/lib/python3.6/dist-packages (from traitlets->cesiumpy) (4.3.0)

群馬県を描写(デフォルト設定)

In [2]:
v = cesiumpy.Viewer()

#geojsonを読み込み座標棟を取得
res = cesiumpy.io.read_geojson('./geojson/prefectures/10.json')

#グラフの値をextrudedHeightとすることで値に応じてポリゴンを浮き出させる
myExtrudedHeight = 10e3

#エンティティーへ設定
v.entities.add(res, extrudedHeight=myExtrudedHeight)

#描写
v
Out[2]:

群馬県を描写(カスタム)

  • 本家のCesium.jsと同様に各種プロパティを変更可能
  • ラッパーが対応していない機能も一部あるようなので注意
In [3]:
#ウィジェットの有無は辞書型で渡すので注意
options = dict(\
               animation=False,\
               timeline=False,\
               fullscreenButton=False,\
              )
In [4]:
v = cesiumpy.Viewer(**options)
res = cesiumpy.io.read_geojson('./geojson/prefectures/10.json')
myExtrudedHeight = 10e3
#各種プロパティの設定
myName = '群馬県'
myDescription = 'I love Gunma'
myMaterial = cesiumpy.color.Color.fromCssColorString('pink').withAlpha(0.8)
myOutlineColor = cesiumpy.color.Color.fromCssColorString('#FF0000')
v.entities.add(\
               res,\
               name = myName,\
#おそらくdescriptionは非対応
               description=myDescription,\
               extrudedHeight = myExtrudedHeight,\
               material = myMaterial,\
               outline  =True,\
               outlineColor = myOutlineColor,\
              )
v
Out[4]:

群馬県を描写(市区町村別)

  • 市区町村単位でGeoJsonを読み込めばよい
  • 元データは行政区域単位で用意されており、行政区域コードがファイル名である
  • 市区町村名と行政区域コードを対応付けたJsonファイルを用意しておくと管理しやすい
In [5]:
import json
import os
import random
In [6]:
with open('./myList.json') as f:
    ml = json.load(f)
f.close()
In [7]:
def setDir(code):
    n = int(code)
    if (n > 1000):
        return code[0:2]
    else:
        return "mergedPrefectures"
In [8]:
def setEntitiy(code,val,rgb1,rgb2):
    path = './geojson/'+setDir(code)+'/'+code+'.json'
    res = cesiumpy.io.read_geojson(path)
    myName = ml[code]
    myMaterial = cesiumpy.color.Color.fromCssColorString(rgb1)
    myOutlineColor = cesiumpy.color.Color.fromCssColorString(rgb2)
    v.entities.add(res,\
                   name=myName,\
                   extrudedHeight=val,\
                   material=myMaterial,\
                   outline=True,\
                   outlineColor=myOutlineColor)
In [9]:
def setRandomEntities(code):
    list = []
    path = 'geojson/'+code
    for file in os.listdir(path):
        base, ext = os.path.splitext(file)
        if ext == '.json':
            list.append(base)
    list.sort()
    for i in list:
        val = random.randrange(10e3)
        clr = '#C'+str(val).zfill(5)
        setEntitiy(i,val,clr,clr)
In [10]:
v = cesiumpy.Viewer(**options)
#今回はランダムな値をsetEntitiy()に代入
setRandomEntities('10')
v
Out[10]:

日本全国を描写(失敗)

  • GeoJsonのデータが大きすぎてブラウザが停止

GeoJsonのサイズダウン

  1. 都道府県境のみを抽出
    • 都道府県のグラフであれば市区町村の区分は不要
    • QGISというGISソフトウェアを利用して結合
    • 手順
      1. QGISにGeoJsonファイルをドラッグ&ドロップ
      2. [ベクター]>[空間演算ツール]>[ディゾルブ]をクリック
      3. [パラメーター]>[入力レイヤ]へ変換したいGeoJsonのレイヤを設定
      4. [パラメーター]>[融合]へ出力先のディレクトリ及びファイル形式(geojson)を設定
      5. [バックグラウンドで実行]をクリック
      6. 複数ある場合には[Run as Batch Process]をクリックして一括処理も可能
      7. 拡張子を「geojson」から「json」に書き換える
  2. GeoJsonの簡素化
    • 結合してもデータ量は大きい
    • ポリゴンの点数そのものを減らすことで処理速度の向上
    • mapshaperサイトというが目視して調整でき利用しやすい(QGISでも簡素化可能)
    • 手順
      1. [mapshaper]にGeoJsonファイルをドラッグ&ドロップ
      2. 必要な[Option]を選択して[Import]
      3. [Simplify]をクリックして、必要な[Simplification menu]を選択
      4. [Settings]の倍率を調整して整形
      5. [Export]をクリック、[GeoJson]を選択
      6. 完了

日本全国を描写

In [11]:
v = cesiumpy.Viewer(**options)
for i in range(1,48):
    val = random.randrange(10e4)
    clr = '#E'+str(val).zfill(5)
    setEntitiy(str(i).zfill(2),val,clr,clr)
v
Out[11]: